﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Shapes;
using System.Windows.Media;

namespace AutomationLib
{
    internal class AutoTestContainer : Window
    {

        internal const string TestWindowName = @"83B34C06-D793-43CF-AB85-6BDACD1C50D9";

        private AutoTestContainer(UIElement objToTest)
        {
            //Content = objToTest;
            Grid grid = new Grid();
            grid.Children.Add(objToTest);

            Rectangle r = new Rectangle();
            r.Fill = new SolidColorBrush(Color.FromArgb(100, 100, 100, 100));
            Binding b = new Binding("ActualWidth");
            b.Source = this;
            r.SetBinding(Rectangle.WidthProperty, b);

            b = new Binding("ActualHeight");
            b.Source = this;
            r.SetBinding(Rectangle.HeightProperty, b);
            grid.Children.Add(r);
            Content = grid;
        }

        internal static Window CreateTestContainer(Type type, params object[] parameters)
        {
            if (type == null)
            {
                string methodName = "AutomationLib.AutoTestContainer.CreateTestContainer";
                throw new ArgumentNullException(methodName + ": type", "the Type of the object to test can not be null");
            }

            object instance;
            if (parameters != null && parameters.Length != 0)
                instance = Activator.CreateInstance(type, parameters);
            else
                instance = Activator.CreateInstance(type);

            return CreateTestContainer(instance as UIElement);
        }

        internal static Window CreateTestContainer(UIElement objectToTest)
        {
            if (objectToTest == null)
            {
                string methodName = "AutomationLib.AutoTestContainer.CreateTestContainer";
                throw new ArgumentNullException(methodName + ": objectToTest", "The given UI object to test can not be null");
            }

            Window container = objectToTest.GetType() == typeof(Window) ?
                               objectToTest as Window :
                               new AutoTestContainer(objectToTest);
            container.Title = TestWindowName;
            return container;
        }
    }
}
